<template>
    <div>
        <el-container>
            <el-header class="homeHeader">
                <h3>博客后台管理系统</h3>

                <el-dropdown @command="userOptions">
                    <span class="el-dropdown-link">
                        {{loginUser.nickname}}<i class="el-icon-arrow-down el-icon--right"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="settings">个人设置</el-dropdown-item>
                        <el-dropdown-item command="center">用户中心</el-dropdown-item>
                        <el-dropdown-item command="logout">注销</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </el-header>
            <el-container>
                <el-aside width="200px">
                    <el-menu router>

                        <el-submenu index="i+''" v-for="(item,i) in this.$router.options.routes"
                                                 v-if="!item.hidden && item.meta && item.meta.sec && checkRoles(item.meta.roles)==200">
                            <template slot="title">
                                <i :class="item.meta.icon"></i>
                                <span>{{item.meta.title}}</span>
                            </template>

                            <div v-for="(child,j) in item.children"
                                 v-if="child.hidden==false">
                                <el-menu-item :index="child.path">
                                    <span slot="title">{{child.meta.title}}</span>
                                </el-menu-item>
                            </div>
                        </el-submenu>

                        <div v-for="(item,i) in this.$router.options.routes"
                             v-if="!item.hidden && !item.meta.sec">
                            <div v-for="(child,j) in item.children"
                                 v-if="checkRoles(child.meta.roles)==200">
                                <el-menu-item :index="child.path">
                                    <i :class="child.meta.icon"></i>
                                    <span slot="title">{{child.meta.title}}</span>
                                </el-menu-item>
                            </div>
                        </div>

                    </el-menu>
                </el-aside>
                <el-main>
                    <el-breadcrumb separator="/" v-if="this.$route.path!='/home'">
                        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                        <el-breadcrumb-item>{{this.$route.meta.title}}</el-breadcrumb-item>
                    </el-breadcrumb>
                    <router-view/>
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script>
    export default {
        name: "Home",
        data() {
            return {
                loginUser:JSON.parse(window.sessionStorage.getItem('loginUser'))
            }
        },
        methods:{
            userOptions(item){
                if(item=='logout'){
                    this.$confirm('确定要注销用户吗, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.$getRequest('/logout').then(resp=>{
                            if(resp){
                                window.sessionStorage.removeItem('loginUser');
                                this.$router.replace('/');
                            }
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消注销'
                        });
                    });
                }
            },
            checkRoles(needRoles){
                let authorities = this.loginUser.authorities;
                let checkRole = false;
                for(let i in needRoles){
                    authorities.forEach((item,j)=>{
                        if(item.authority == needRoles[i]){
                            checkRole = true;
                        }
                    })
                }
                if(checkRole){
                    return 200;
                }else{
                    return 500;
                }
            }
        }
    }
</script>

<style scoped>

    .homeHeader h3{
        color: white;
    }

    .homeHeader{
        background-color: #409EFF;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .el-dropdown-link {
        cursor: pointer;
        color: #fff;
    }
    .el-icon-arrow-down {
        font-size: 12px;
    }
</style>